package com.sl.springboot.controller;

import com.sl.springboot.model.*;
import com.sl.springboot.service.AdminService;
import com.sl.springboot.service.CoachService;
import com.sl.springboot.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
public class CoachController {

    // 依赖注入
    @Autowired
    private CoachService coachService;
    @Autowired
    private OrdersService ordersService;


    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findCoach")
    public String findCoach(Integer pageIndex, Integer pageSize, Model model,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        PageInfo<Coach> pageList = coachService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "CoachList";
    }


    /**
     * 添加
     */
    @RequestMapping(value = "/addCoach" ,method = RequestMethod.POST)
    @ResponseBody
    public String addCoach( @RequestBody Coach coach) {
        int d = coachService.addCoach(coach);
        return "AdminList";
    }


    /**
     * 删除
     */
    @RequestMapping( "/deleteCoach")
    @ResponseBody
    public String deleteCoach(String id) {
        int d = coachService.deleteCoach(id);
        return "CoachList";
    }


    /**
     * 修改
     */
    @RequestMapping( "/updateCoach")
    public String updateCoach( Coach coach) {
        int d = coachService.updateCoach(coach);
        return "redirect:/findCoach";
    }


    /**
     * 按照ID查询
     */
    @RequestMapping( "/findCoachById")
    public String findCoachById(String id,Model model,HttpServletRequest request) {
        Coach coach= coachService.findCoachById(id);
        model.addAttribute("coach",coach);
        return "CoachEdit";
    }

    @RequestMapping(value = "/findCoachOrders")
    public String findOrders(Integer pageIndex, Integer pageSize ,String cname,String uname, Model model,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        String type = (String)session.getAttribute("type");
        Map mp = new HashMap();
        mp.put("cname",cname);
        mp.put("uname",uname);
        if(type.equals("02")){
            User u = (User)session.getAttribute("ad");
            mp.put("uid",u.getId());
        }

        PageInfo<Orders> pageList = ordersService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "OrdersList";
    }


}
